package com.purengage.ipilih;

import com.purengage.ipilih.lib.DBClass;
import com.purengage.ipilih.lib.DialogBox;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

@SuppressWarnings("deprecation")
public class Main extends TabActivity {
	private TabHost mTabHost;

	private void setupTabHost() {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
	}

	public void onCreate(Bundle savedInstanceState) {
		if(Build.VERSION.SDK_INT >= 11)
			setTheme(android.R.style.Theme_Holo_Light_NoActionBar);
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		DBClass db = new DBClass(this);
		int msg = db.getUnreadMsg();
		
		Log.i("--count--", ""+msg);
		
		int sts = R.drawable.status_w;

		setupTabHost();
		
		setupTab(new TextView(this), "Status", "Status.Class", sts);
		
		setupTab(new TextView(this), "Survey", "Survey.class",
				R.drawable.survey_w);

		setupTab(new TextView(this), "Relawan", "Relawan.class",
				R.drawable.responden_w);
		
		setupTab(new TextView(this), "More", "More.class", R.drawable.more);
		
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			int index = extras.getInt("index");		
			mTabHost.setCurrentTab(index);
		}
		  
	}

	private void setupTab(final View view, final String tag,
			final String className, final int drawable) {
		
		View tabview = createTabView(mTabHost.getContext(), tag, drawable);

		Intent intent;
		intent = new Intent().setClass(this, Status.class);
		
		if (className.equals("Status.class")) {
			intent = new Intent().setClass(this, Status.class);
		}
		if (className.equals("Survey.class")) {
			intent = new Intent().setClass(this, Survey.class);
		}

		if (className.equals("Relawan.class")) {
			intent = new Intent().setClass(this, Relawan.class);
		}

		if (className.equals("More.class")) {
			intent = new Intent().setClass(this, More.class);
		}

		TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
				.setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

		mTabHost.addTab(setContent);
	}

	private static View createTabView(final Context context, final String text,
			final int drawable) {
		View view = LayoutInflater.from(context)
				.inflate(R.layout.tabs_bg, null);
		TextView tv = (TextView) view.findViewById(R.id.tabsText);
		ImageView iv = (ImageView) view.findViewById(R.id.imgTab);
		tv.setText(text);
		iv.setImageResource(drawable);
		return view;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_bantuan_teknis:
			DialogBox dbox = new DialogBox(Main.this);
			String support_num = getResources().getString(
					R.string.support_phone_number);
			dbox.PoupupDialog("Butuh Bantuan ?", "Silahkan hubungi koordinator survey anda di nomor berikut: "+ support_num);
			return true;
		case R.id.action_keluar:
			return true;
		default:
			return false;
		}
	}
}
